# coding:utf-8
import tkinter as tk
import tkinter.messagebox as tkMess
import random


def onclick():
    global inputVal
    inputAgeValue = inputVal.get()
    if not inputAgeValue.isdigit():
        tkMess.showerror("错误", "你输入的年龄有误")
        return
    inputAge = int(inputAgeValue)
    if inputAge <= 0 or inputAge > 100:
        tkMess.showwarning("警告", "你输入的年龄超出了范围")
        return
    global age, tipStr, tipVal, minAge, maxAge
    if inputAge > age:
        maxAge = inputAge - 1
        tkMess.showinfo("提示", "猜大了！")
    elif inputAge == age:
        tkMess.showinfo("提示", "恭喜你，猜中了！")
    else:
        minAge = inputAge + 1
        tkMess.showinfo("提示", "猜小了！")
    tipVal.set(tipStr.format(minAge, maxAge))


if __name__ == "__main__":
    window = tk.Tk()
    window.title("窗口程序")
    window.geometry("300x200")

    age = random.randint(1, 100)

    row = tk.Frame(window)
    label = tk.Label(row, text="年龄：")
    label.pack(side=tk.LEFT)
    inputVal = tk.StringVar()
    input = tk.Entry(row, textvariable=inputVal)
    input.pack(side=tk.RIGHT)
    row.pack(pady=(50, 5))

    tipStr = "请输入{}-{}之间的年龄"
    minAge = 0
    maxAge = 100
    tipVal = tk.StringVar()
    tipVal.set(tipStr.format(minAge, maxAge))
    label = tk.Label(window, textvariable=tipVal)
    label.pack()

    button = tk.Button(window, text="确定", command=onclick)
    button.pack(pady=15)

    window.mainloop()
